'use strict';
	angular.module('inventory.fileItemSelectMulti', [])
	.controller('fileItemSelectMultiCtrl'
			,['$rootScope', '$scope','systemService', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, systemService, BizData, $uibModalInstance, params, title) {


		var Model = this;
		
		Model.gridRowClick = function(event){
			
		}

		Model.gridRowDblClick = function(event){
			Model.ok();
		}
				
		var options = {};
		options.container = Model;
		options.concept = 'FileItem';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
 		options.reader = '/inventory/fileItem/list/query';
		options.writer = '/inventory/fileItem/list/save';
		options.deleter = '/inventory/fileItem/list/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.nodeSortRelation = "sortNo";
		options.rootFilter = '';
//		options.pathRelation = 'itemFullID';
		options.directDeleteMode = true;
		options.limit = 10;
		options.orderBy = "sortNo ASC, itemNo ";
		
		$scope.dataList = new BizData(options);

		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
        	Model.title = title;  
        	Model.params = params;  
        	
    		//设置过滤条件
    		$scope.dataList.filter = "FileItem.enterpriseNo = '"+ Model.params.id +"'"
    		+ " AND FileItem.status='open' ";
    		
    		//这里只能是不可配置物料，不用考虑可配置
    		/*
    		if(params.configType){
    			var configType = params.configType;
    			$scope.dataList.filter = $scope.dataList.filter + " AND FileItem.configType="+configType;
    		}
    		*/
    		//这里只能是不可配置物料，不用考虑可配置
			$scope.dataList.filter = $scope.dataList.filter + " AND FileItem.configType=0";
    		
    		/*
    		if(params.kind=='nkLeaf'){
    			//只显示叶子
        		$scope.dataList.treeOption.rootFilter = '';
        		$scope.dataList.filter = $scope.dataList.filter + " AND FileItem.nodeKind='nkLeaf'"
        		;
    		}
    		*/
    		
    		//刷新数据
    	    $scope.dataList.refreshData();
        });  
        
		
        Model.ok = function () { 
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
	    	for(var index=0;index<$scope.dataList.rows.length;index++){
	    		if($scope.dataList.rows[index].userdata.checked){
	    	    	
	    	    	var sourceRow = $scope.dataList.rows[index];
	    	    	var row = {};
	    			//循环行中的列，取出列的值，装配成简单的行
	    			for(var obj in sourceRow){
	    				if(obj!='$$hashKey' && obj!='userdata'){
	    					row[obj] = sourceRow[obj].value;
	    				}
	    			}
	    	    	
	    	    	data.push(row);
	    			
	    		}
	    	}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    

		//表格标题中的选择框ng-model
		$scope.checkboxValue = false;
		//表格标题中的选择对话框click事件
		Model.checkboxClick = function () {  
			for(var index=0;index<$scope.dataList.rows.length;index++){
				/*
		  		//判断规格问题（树形才需要判断）
		  		var nodeKind = $scope.dataList.rows[index].nodeKind.value;
		  		if(Model.params.kind=='nkTree' || Model.params.kind==null){
		  			if(nodeKind=='nkLeaf'){
				  		$scope.dataList.rows[index].userdata.checked=$scope.checkboxValue;
			  		}
		  		}else{
			  		$scope.dataList.rows[index].userdata.checked=$scope.checkboxValue;
		  		}
		  		*/
		  		$scope.dataList.rows[index].userdata.checked=$scope.checkboxValue;
		 	}
		};    

		//////////////////////对话框使用结束//////////////////////////////////////

	    return Model;
	    
	}	
	
